Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: testgen-hs – random Haskell CBOR error generator #71

Closed
wants to merge 5 commits into from

Conversation

michalrus
Copy link
Collaborator

@michalrus michalrus commented Nov 15, 2024

Related to #42.

Related to @michalrus' Pallas PR fixing a bug this PR uncovered:

Status

*DO NOT MERGE* – we’ll have a separate repository hooked up to IOG Hydra.

I'll modify this PR to use the static build from there.

Example 1 – how to use it

unfold
❯ cd Work/blockfrost-platform

🔨 Welcome to blockfrost-platform-devshell

[…]

[handy]
  testgen-hs       - Test case generator for cross-checking CBOR (de)serializers
❯ testgen-hs --help

Usage: testgen-hs COMMAND

  Test case generator for cross-checking CBOR (de)serializers

Available options:
  -h,--help                Show this help text

Available commands:
  generate                 Generate random CBOR test cases
  deserialize              Deserialize CBOR of ‘HardForkApplyTxErr’ that you got
                           from cardano-node
❯ testgen-hs generate --help

Usage: testgen-hs generate [-s|--seed SEED] [-g|--generator-size SIZE]
                           [-n|--number NUM] COMMAND

  Generate random CBOR test cases

Available options:
  -s,--seed SEED           Random seed integer (UNIX timestamp by default)
  -g,--generator-size SIZE Set the relative ‘size’ of the test cases
  -n,--number NUM          How many test cases to generate
  -h,--help                Show this help text

Available commands:
  ApplyTxErr_Byron         Generate CBOR of ApplyTxErr'Byron
  ApplyTxErr_Shelley       Generate CBOR of ApplyTxErr'Shelley
  ApplyTxErr_Allegra       Generate CBOR of ApplyTxErr'Allegra
  ApplyTxErr_Mary          Generate CBOR of ApplyTxErr'Mary
  ApplyTxErr_Alonzo        Generate CBOR of ApplyTxErr'Alonzo
  ApplyTxErr_Babbage       Generate CBOR of ApplyTxErr'Babbage
  ApplyTxErr_Conway        Generate CBOR of ApplyTxErr'Conway
  GHCInteger               Generate CBOR of GHCInteger
  DataText                 Generate CBOR of DataText
  ExampleADT               Generate CBOR of ExampleADT
❯ testgen-hs generate -g1 -n1 ApplyTxErr_Conway
{
  "seed": 1731690796,
  "testCases": [
    {
      "cbor": "81820682820182018082038210581de0988807a48f6c7871bfa29d3ea70a6328c5fa35beb8c48589c676dc7b",
      "haskellRepr": "ApplyTxError (ConwayUtxowFailure (InvalidWitnessesUTXOW []) :| [ConwayGovFailure (ProposalReturnAccountDoesNotExist (RewardAccount {raNetwork = Testnet, raCredential = KeyHashObj (KeyHash {unKeyHash = \"988807a48f6c7871bfa29d3ea70a6328c5fa35beb8c48589c676dc7b\"})}))])",
      "json": {
        "contents": {
          "contents": {
            "contents": {
              "era": "ShelleyBasedEraConway",
              "error": [
                "ConwayUtxowFailure (InvalidWitnessesUTXOW [])",
                "ConwayGovFailure (ProposalReturnAccountDoesNotExist (RewardAccount {raNetwork = Testnet, raCredential = KeyHashObj (KeyHash {unKeyHash = \"988807a48f6c7871bfa29d3ea70a6328c5fa35beb8c48589c676dc7b\"})}))"
              ],
              "kind": "ShelleyTxValidationError"
            },
            "tag": "TxValidationErrorInCardanoMode"
          },
          "tag": "TxCmdTxSubmitValidationError"
        },
        "tag": "TxSubmitFail"
      },
      "typeTag": "ApplyTxError (ConwayEra StandardCrypto)"
    }
  ]
}

Example 2 – searching for a particular error

unfold

With a little of Bash, it's possible to search for a particular error:

while true ; do
    output=$(testgen-hs generate -g1 -n1 ApplyTxErr_Conway)
    if grep -q FeeTooSmall <<<"$output" ; then
      jq . <<<"$output"
      break
    fi
    sleep 0.01
  done
{
  "seed": 1732539592414,
  "testCases": [
    {
      "cbor": "81820682830601008201820083051a000aa8a91a000291ff",
      "haskellRepr": "ApplyTxError (ConwayTxRefScriptsSizeTooBig 1 0 :| [ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 698537) (Coin 168447)))])",
      "json": {
        "contents": {
          "contents": {
            "contents": {
              "era": "ShelleyBasedEraConway",
              "error": [
                "ConwayTxRefScriptsSizeTooBig 1 0",
                "ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 698537) (Coin 168447)))"
              ],
              "kind": "ShelleyTxValidationError"
            },
            "tag": "TxValidationErrorInCardanoMode"
          },
          "tag": "TxCmdTxSubmitValidationError"
        },
        "tag": "TxSubmitFail"
      },
      "typeTag": "ApplyTxError (ConwayEra StandardCrypto)"
    }
  ]
}

And then easily reproduce it by providing the same seed that was found:

❯ testgen-hs generate -s1732539592414 -g1 -n1 ApplyTxErr_Conway
{
  "seed": 1732539592414,
  "testCases": [
    {
      "cbor": "81820682830601008201820083051a000aa8a91a000291ff",
      "haskellRepr": "ApplyTxError (ConwayTxRefScriptsSizeTooBig 1 0 :| [ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 698537) (Coin 168447)))])",
      "json": {
        "contents": {
          "contents": {
            "contents": {
              "era": "ShelleyBasedEraConway",
              "error": [
                "ConwayTxRefScriptsSizeTooBig 1 0",
                "ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 698537) (Coin 168447)))"
              ],
              "kind": "ShelleyTxValidationError"
            },
            "tag": "TxValidationErrorInCardanoMode"
          },
          "tag": "TxCmdTxSubmitValidationError"
        },
        "tag": "TxSubmitFail"
      },
      "typeTag": "ApplyTxError (ConwayEra StandardCrypto)"
    }
  ]
}

Example 3 – two small Conway errors

unfold
❯ testgen-hs generate -s1732187056519 -g1 -n1 ApplyTxErr_Conway
{
  "seed": 1732187056519,
  "testCases": [
    {
      "cbor": "818206828201820083051a000151351a00074b8582076162",
      "haskellRepr": "ApplyTxError (ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 86325) (Coin 478085))) :| [ConwayMempoolFailure \"b\"])",
      "json": {
        "contents": {
          "contents": {
            "contents": {
              "era": "ShelleyBasedEraConway",
              "error": [
                "ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 86325) (Coin 478085)))",
                "ConwayMempoolFailure \"b\""
              ],
              "kind": "ShelleyTxValidationError"
            },
            "tag": "TxValidationErrorInCardanoMode"
          },
          "tag": "TxCmdTxSubmitValidationError"
        },
        "tag": "TxSubmitFail"
      },
      "typeTag": "ApplyTxError (ConwayEra StandardCrypto)"
    }
  ]
}
❯ testgen-hs generate -s1732186210861 -g1 -n1 ApplyTxErr_Conway
{
  "seed": 1732186210861,
  "testCases": [
    {
      "cbor": "818206828201820083051a000a63d11a0009580683051a0008d00a1a0001ebc4",
      "haskellRepr": "ApplyTxError (ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 680913) (Coin 612358))) :| [ConwayTreasuryValueMismatch (Coin 577546) (Coin 125892)])",
      "json": {
        "contents": {
          "contents": {
            "contents": {
              "era": "ShelleyBasedEraConway",
              "error": [
                "ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 680913) (Coin 612358)))",
                "ConwayTreasuryValueMismatch (Coin 577546) (Coin 125892)"
              ],
              "kind": "ShelleyTxValidationError"
            },
            "tag": "TxValidationErrorInCardanoMode"
          },
          "tag": "TxCmdTxSubmitValidationError"
        },
        "tag": "TxSubmitFail"
      },
      "typeTag": "ApplyTxError (ConwayEra StandardCrypto)"
    }
  ]
}

Example 4 – deserialize a CBOR

unfold
❯ testgen-hs deserialize 818206828201820083051a000a63d11a0009580683051a0008d00a1a0001ebc4
{
  "cbor": "818206828201820083051a000a63d11a0009580683051a0008d00a1a0001ebc4",
  "haskellRepr": "ApplyTxError (ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 680913) (Coin 612358))) :| [ConwayTreasuryValueMismatch (Coin 577546) (Coin 125892)])",
  "json": {
    "contents": {
      "contents": {
        "contents": {
          "era": "ShelleyBasedEraConway",
          "error": [
            "ConwayUtxowFailure (UtxoFailure (FeeTooSmallUTxO (Coin 680913) (Coin 612358)))",
            "ConwayTreasuryValueMismatch (Coin 577546) (Coin 125892)"
          ],
          "kind": "ShelleyTxValidationError"
        },
        "tag": "TxValidationErrorInCardanoMode"
      },
      "tag": "TxCmdTxSubmitValidationError"
    },
    "tag": "TxSubmitFail"
  },
  "typeTag": "ApplyTxError (ConwayEra StandardCrypto)"
}

@michalrus michalrus added this to the v0.0.1 milestone Nov 15, 2024
@michalrus michalrus self-assigned this Nov 15, 2024
@blockfrost blockfrost deleted a comment from vercel bot Nov 16, 2024
@ginnun
Copy link
Collaborator

ginnun commented Nov 21, 2024

This can be a good candidate for a standalone project, since can be useful for many other.

@michalrus michalrus force-pushed the feat/haskell-error-generator branch 2 times, most recently from 75582ac to c01571c Compare November 22, 2024 08:41
@michalrus michalrus force-pushed the feat/haskell-error-generator branch 2 times, most recently from fc9c661 to ff0db8e Compare November 25, 2024 09:25
@michalrus michalrus marked this pull request as ready for review November 25, 2024 09:26
src/node/connection.rs Outdated Show resolved Hide resolved
src/node/connection.rs Outdated Show resolved Hide resolved
@michalrus michalrus force-pushed the feat/haskell-error-generator branch 2 times, most recently from a681f3e to 4372308 Compare November 25, 2024 12:54
@michalrus michalrus changed the title feat: initial commit of testgen-hs, random Haskell error generator feat: testgen-hs – random Haskell error generator Nov 25, 2024
@michalrus michalrus changed the title feat: testgen-hs – random Haskell error generator feat: testgen-hs – random Haskell CBOR error generator Nov 25, 2024
@michalrus michalrus marked this pull request as draft November 26, 2024 11:14
@michalrus
Copy link
Collaborator Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants